<template>
  <view class="container">
    <view><button class="login" open-type="getPhoneNumber" @getphonenumber="getusernumbertoken">手机号授权登录</button></view>
    <view style="padding: 20rpx 50rpx">
      <label class="radio">
        <radio class="check" color="#0398FF" :checked="checkstate" @click="changecheckstate" />
      </label>
      <text style="font-size: 26rpx">
        我已阅读并同意
        <text class="textcolor" @click="gouseragreement">《用户协议》</text>
        和
        <text class="textcolor" @click="goparivacy">《隐私政策》</text>
      </text>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      checkstate: false
    };
  },
  methods: {
    // 跳转到用户协议
    gouseragreement() {
      uni.navigateTo({
        url: '/sub_my/userservice/userservice'
      });
    },
    // 跳转到隐私政策
    goparivacy() {
      uni.navigateTo({
        url: '/sub_my/userprofile/userprofile'
      });
    },
    // 用户点击登录
    getusernumbertoken(e) {
      if (!this.checkstate) {
        return uni.showToast({
          title: '请先勾选协议',
          icon: 'none'
        });
      }
      if (!e.detail.code) {
        uni.showToast({
          title: '获取手机号失败',
          icon: 'none'
        });
        return false;
      }
      console.log(e.detail);
      uni.login({
        success: async (res) => {
          console.log(res);
          // 调取登录接口
          uni.request({
            // url: 'https://health.jiangkukeji.cn/api/user/mobilelogin', // 测试使用
            url: 'https://www.gansuyiliao.cn/api/user/mobilelogin', // 正式使用
            method: 'POST',
            data: {
              code: res.code, // login的code
              encrypted_data: e.detail.encryptedData, // 用户信息
              iv: e.detail.iv,
              phone_code: e.detail.code
            },
            success: (res2) => {
              this.userinfo = res2.data.data.userinfo;
              uni.setStorageSync('token', res2.data.data.userinfo.token);
              uni.switchTab({
                url: '/pages/my/my'
              });
            }
          });
        }
      });
    },
    // 用户点击是否勾选协议
    changecheckstate() {
      this.checkstate = !this.checkstate;
    }
  }
};
</script>

<style lang="scss" scoped>
.container {
  .textcolor {
    color: #0398ff;
  }
  .login {
    width: 650rpx;
    height: 90rpx;
    margin-top: 300rpx;
    text-align: center;
    line-height: 90rpx;
    color: #fff;
    background: #0274f9;
    border-radius: 100rpx 100rpx 100rpx 100rpx;
  }

  .check {
    transform: scale(0.7);
  }
}
</style>
